package gui.charts;

import java.util.LinkedList;

import javax.swing.JPanel;

import lbd.TermFreq;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;

/**
 * Create a pie chart using data from DefaultPieDataset.
 */
public abstract class Chart{

	private static final long serialVersionUID = 1L;
	protected LinkedList<TermFreq> termList;

	/**
     * Default constructor.
     *
     * @param title  the frame title.
     */
    public Chart(LinkedList<TermFreq> termList) {
    	
        this.termList = termList;
        
    }

    /**
     * Creates a sample dataset.
     *
     * @return A sample dataset.
     */
    protected PieDataset createDataset() {
    	
        DefaultPieDataset dataset = new DefaultPieDataset();
        
        for( int i = 0 ; i < termList.size() ; i++ ) {
        	
        	dataset.setValue(	termList.get(i).getTerm(),
        						termList.get(i).getFreq());
        	
        }
        
        return dataset;
    }

    /**
     * Creates a chart.
     *
     * @param dataset  the dataset.
     *
     * @return A chart.
     */
    protected abstract JFreeChart createChart(PieDataset dataset);

    /**
     * Creates a panel for the demo (used by SuperDemo.java).
     *
     * @return A panel.
     */
    public JPanel createPanel() {
        JFreeChart chart = createChart(createDataset());
        ChartPanel chartPanel = new ChartPanel(chart,400,200,500,500,750,750,true,true,true,true,true,true);
        return chartPanel;
    }

}

